//package com.ztom.agent.report;
//
//import com.alibaba.fastjson.JSONObject;
//import com.ztom.agent.common.SystemProperties;
//import com.ztom.agent.log.Logger;
//import com.ztom.agent.util.ExecutorManager;
//import com.ztom.agent.util.JarToolUtils;
//import com.ztom.agent.util.ThreadUtils;
//
//import java.nio.charset.StandardCharsets;
//import java.nio.file.Files;
//import java.nio.file.Path;
//import java.nio.file.Paths;
//import java.nio.file.StandardOpenOption;
//import java.util.List;
//import java.util.concurrent.ScheduledThreadPoolExecutor;
//import java.util.concurrent.ThreadPoolExecutor;
//import java.util.concurrent.TimeUnit;
//
///**
// * @author ZhangTao
// */
//public class ReportScheduler {
//
//    private static final ScheduledThreadPoolExecutor SCHEDULED_EXECUTOR = new ScheduledThreadPoolExecutor(
//            2, ThreadUtils.newThreadFactory("MyTraceAgent-ReportScheduler-"), new ThreadPoolExecutor.DiscardPolicy());
//
//    private final long initialDelay;
//
//    private final long period;
//
//    private final TimeUnit unit;
//
//    public ReportScheduler(long initialDelay, long period, TimeUnit unit) {
//        this.initialDelay = initialDelay;
//        this.period = period;
//        this.unit = unit;
//    }
//
//    public static void initScheduleTask() {
//        ExecutorManager.addExecutorService(SCHEDULED_EXECUTOR);
//
//        new ReportScheduler(0, 10, TimeUnit.MILLISECONDS).start();
//    }
//
//    private void start() {
//        SCHEDULED_EXECUTOR.scheduleAtFixedRate(
//                () -> {
//                    try {
//                        List<Record> records = MessageQueue.getInstance().batchGet();
//                        StringBuilder builder = new StringBuilder();
//                        for (Record record : records) {
//                            SpanModel spanModel = (SpanModel) record;
//                            builder.append(JSONObject.toJSONString(spanModel)).append(SystemProperties.LINE_SEPARATOR);
//                        }
//                        String path = JarToolUtils.getJarCurrentDir() + "/test/result.log";
//                        Path file = Paths.get(path);
//                        if (!Files.exists(file)) {
//                            Files.createDirectories(file.getParent());
//                        }
//                        Files.write(file, builder.toString().getBytes(StandardCharsets.UTF_8),
//                                StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.APPEND);
//                    } catch (Exception e) {
//                        Logger.error("ReportScheduler.start()", e);
//                    }
//                }, initialDelay, period, unit);
//    }
//}
